import { isNil } from 'lodash';
import { AuthGuard } from "@/common/guard/auth.guard";
import { Body, Controller, Post, Session, UseGuards } from "@nestjs/common";
import { CfsService } from "./cfs.service";
import { DeleteDto, SqlQueryDto } from "@/common/type/app.dto";
import { QueryCntrNoDto, QueryPlanCntrsDto } from '@/container/dto/create-container.dto';
import { CntrFixItemDto, DeleteCntrFixItemsDto, GateInCntrDto, GateOutCntrDto, RentInCntrDto, UpdateCntrFixItemDto } from './dto/cfs.dto';

@Controller('cfs')
@UseGuards(AuthGuard)
export class CfsController {
    constructor(private readonly cfsService: CfsService) { }
    private getCfs(session: Record<string, any>): number {
        const cfs_id = session['user']['cfs_id'];
        if (isNil(cfs_id)) {
            throw new Error("当前用户没有指定站点");
        }
        return cfs_id;
    }
    @Post('cntrquery')
    async getCntr(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryCntr(query, cfs_id);
    }
    @Post('exportcntrquery')
    async exportGetCntr(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.exportQueryCntr(query, cfs_id);
    }

    @Post('inplanquery')
    async getInPlan(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryCntrInPlan(query, cfs_id);
    }
    @Post('outplanquery')
    async getOutPlan(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryCntrOutPlan(query, cfs_id);
    }
    @Post('leaseplanquery')
    async getLeasePlan(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryCntrLeasePlan(query, cfs_id);
    }
    /* @Post('rentplanquery')
    async getRentPlan(@Body() query: SqlQueryDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryCntrRentPlan(query, cfs_id);
    } */
    @Post('planunsedprecntrs')
    async getUsedPreCntrs(@Body() query: QueryPlanCntrsDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.queryUnusedPreCntrsByPlan(query, cfs_id);
    }

    @Post('gatein')
    async gatein(@Body() dto: GateInCntrDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.gatein(dto, cfs_id, oper_nam);
    }
    
    @Post('gateout')
    async gateout(@Body() dto: GateOutCntrDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.gateout(dto, cfs_id, oper_nam);
    }
    @Post('leasegateout')
    async leasegateout(@Body() dto: GateOutCntrDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.leasegateout(dto, cfs_id, oper_nam);
    }
    @Post('rentgatein')
    async rentgatein(@Body() dto: RentInCntrDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.rentgatein(dto, cfs_id, oper_nam);
    }
    @Post('transgatein')
    async transgatein(@Body() dto: RentInCntrDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.transgatein(dto, cfs_id, oper_nam);
    }
    @Post('getfixbycntrno')
    async getFix(@Body() dto: QueryCntrNoDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.getFixListByCntr(dto.cntr_no, cfs_id);
    }

    @Post('createcntrfixitem')
    async createcntrfixitem(@Body() dto: CntrFixItemDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.createCntrFixItem(dto, cfs_id);
    }

    @Post('updatecntrfixitem')
    async updatecntrfixitem(@Body() dto: UpdateCntrFixItemDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.updateCntrFixItem(dto, cfs_id);
    }

    @Post('deletecntrfixitems')
    async deletecntrfixitems(@Body() dto: DeleteCntrFixItemsDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        return await this.cfsService.deleteCntrFixItems(dto, cfs_id);
    }

    @Post('quotecntrfix')
    async quotecntrfix(@Body() dto: DeleteDto, @Session() session: Record<string, any>) {
        const cfs_id = this.getCfs(session);
        const oper_nam = session['user']['name'];
        return await this.cfsService.QuoteCntrFix(dto, cfs_id, oper_nam);
    }
}